﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

/// <summary>
/// 包含扩展方法的类。
/// </summary>
public static class Extension
{
	#region bool IsMobile(this string value, ...) 指示该字符串是否符合手机号格式（1开头的11位数字）

	/// <summary>
	/// 指示该字符串是否符合手机号格式（1开头的11位数字）
	/// </summary>
	/// <param name="value"></param>
	/// <param name="pattern">要匹配的正则表达式模式</param>
	/// <param name="options">枚举值的一个按位组合，这些枚举值提供匹配选项</param>
	/// <returns></returns>
	public static bool IsMobile(this string value, string pattern = @"^1[1-9]\d{9}$", RegexOptions options = RegexOptions.None)
	{
		bool result = false;

		if (Regex.IsMatch(value, pattern, options))
			result = true;

		return result;
	}

	#endregion

	#region int ConvertInt(...) 将字符串转换为Int类型。

	/// <summary>
	/// 将字符串转换为Int类型。
	/// <para>转换失败或异常时返回 defaultValue。</para>
	/// </summary>
	/// <param name="value"></param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static int ConvertInt(this string value, int defaultValue = 0)
	{
		int result = 0;

		if (!int.TryParse(value, out result))
			result = defaultValue;

		return result;
	}

	/// <summary>
	/// 将字符串转换为Int类型。
	/// </summary>
	/// <param name="value"></param>
	/// <returns></returns>
	/// <exception cref="ArgumentException"></exception>
	/// <exception cref="ArgumentNullException"></exception>
	public static int ConvertInt(this string value)
	{
		if (string.IsNullOrWhiteSpace(value))
			throw new ArgumentException("参数“value”不能为空引用或空字符串。");

		int result = 0;

		if (!int.TryParse(value, out result))
			throw new ArgumentException("参数“value”转换为 Int32 失败。", "value");

		return result;
	}

	#endregion

	#region decimal ConvertDecimal(...) 将字符串转换为Decimal类型。

	/// <summary>
	/// 将字符串转换为Decimal类型。
	/// <para>转换失败或异常时返回 defaultValue。</para>
	/// </summary>
	/// <param name="value"></param>
	/// <param name="defaultValue">默认值</param>
	/// <returns></returns>
	public static decimal ConvertDecimal(this string value, decimal defaultValue = 0)
	{
		decimal result = 0;

		if (!decimal.TryParse(value, out result))
			result = defaultValue;

		return result;
	}

	/// <summary>
	/// 将字符串转换为Decimal类型。
	/// </summary>
	/// <param name="value"></param>
	/// <returns></returns>
	/// <exception cref="ArgumentException"></exception>
	/// <exception cref="ArgumentNullException"></exception>
	public static decimal ConvertDecimal(this string value)
	{
		if (string.IsNullOrWhiteSpace(value))
			throw new ArgumentException("参数“value”不能为空引用或空字符串。");

		decimal result = 0;

		if (!decimal.TryParse(value, out result))
			throw new ArgumentException("参数“value”转换为 Decimal 失败。", "value");

		return result;
	}

	#endregion

	#region List<string> ConvertListString(this string value, char separator = '|', bool clearEmpty = false) 将字符串转换为泛型

	/// <summary>
	/// 将字符串转换为泛型。
	/// <para>字符串格式为：“字符1|字符2|...”，分割线与separator参数匹配。</para>
	/// </summary>
	/// <param name="value"></param>
	/// <param name="separator">分隔此实例中子字符串的Unicode字符数组、不包含分隔符的空数组或null</param>
	/// <param name="clearEmpty">是否清除空字符串</param>
	/// <returns></returns>
	public static List<string> ConvertListString(this string value, char separator = '|', bool clearEmpty = false)
	{
		List<string> infos = new List<string>();

		if (!string.IsNullOrWhiteSpace(value))
		{
			foreach (string val in value.Split(separator))
			{
				if (clearEmpty && !string.IsNullOrWhiteSpace(val))
					infos.Add(val);

				else if (!clearEmpty)
					infos.Add(val);
			}
		}

		return infos;
	}

	#endregion

	#region MinerManager.Models.MinerHashrateAndTmpFanModel ConvertHashrate(this MinerManager.Models.MinerRedisData redisData) 将RedisData数据转换为算力、风扇、温度等信息。

	/// <summary>
	/// 将RedisData数据转换为算力、风扇、温度等信息。
	/// </summary>
	/// <param name="redisData"></param>
	/// <returns></returns>
	public static MinerManager.Models.MinerHashrateAndTmpFanModel ConvertHashrate(this MinerManager.Models.MinerRedisData redisData)
	{
		MinerManager.Models.MinerHashrateAndTmpFanModel result = new MinerManager.Models.MinerHashrateAndTmpFanModel();

		List<string> ta = redisData.Ta.ConvertListString(';');
		List<string> tb = redisData.Tb.ConvertListString(';');
		List<string> sa = redisData.Sa.ConvertListString(';');
		List<string> sb = redisData.Sb.ConvertListString(';');
		List<string> tmpFan = redisData.TmpFan.ConvertListString(';');

		decimal totalTemp = 0, totalFan = 0;
		int num = sa.Count > sb.Count ? sa.Count : sb.Count;
		for (int i = 0; i < num; i++)
		{
			string sai = sa.Count > i ? sa[i] : "0";
			string sbi = sb.Count > i ? sb[i] : "off";

			string firstHashrate = !sai.Equals("Off", StringComparison.CurrentCultureIgnoreCase) ? (sai.ConvertDecimal() / 1000).ToString("0.00") + "MH/S" : sai;
			string secondHashrate = !sbi.Equals("Off", StringComparison.CurrentCultureIgnoreCase) ? (sbi.ConvertDecimal() / 1000).ToString("0.00") + "MH/S" : sbi;
			string tmp = tmpFan.Count > i * 2 ? tmpFan[i * 2] + "℃" : "-";
			string fan = tmpFan.Count > i * 2 + 1 ? tmpFan[i * 2 + 1] + "%" : "-";

			totalTemp += tmpFan.Count > i * 2 ? tmpFan[i * 2].ConvertDecimal() : 0;
			totalFan += tmpFan.Count > i * 2 + 1 ? tmpFan[i * 2 + 1].ConvertDecimal() : 0;

			result.FirstHashrate.Add(firstHashrate);
			result.SecondHashrate.Add(secondHashrate);
			result.Temps.Add(tmp);
			result.Fans.Add(fan);
		}

		result.FirstTotalHashrate = (ta[0].ConvertDecimal() / 1000).ToString("0.000") + "MH/S";
		result.FirstSuccess = ta[1];
		result.FirstFailure = ta[2];

		result.SecondTotalHashrate = (tb[0].ConvertDecimal() / 1000).ToString("0.000") + "MH/S";
		result.SecondSuccess = tb[1];
		result.SecondFailure = tb[2];

		result.TempAvg = num > 0 ? (totalTemp / num).ToString("0") + "℃" : "-";
		result.FanAvg = num > 0 ? (totalFan / num).ToString("0") + "%" : "-";

		return result;
	}

	#endregion

	#region MinerManager.Models.MinerModel ConvertMinerModel(this McsUtility.Models.MinersModel info)

	/// <summary>
	/// 将<see cref="McsUtility.Models.MinersModel"/>转换为<see cref="MinerManager.Models.MinerModel"/>。
	/// </summary>
	/// <param name="info"></param>
	/// <returns></returns>
	public static MinerManager.Models.MinerModel ConvertMinerModel(this McsUtility.Models.MinersModel info)
	{
		MinerManager.Models.MinerModel result = new MinerManager.Models.MinerModel();

		if (info != null)
		{
			result = new MinerManager.Models.MinerModel
			{
				ID = info.ID,
				UserID = info.UserID,
				MacAddress = info.MacAddress,
				Second = info.Second,
				AutoName = info.AutoName,
				AutoRun = info.AutoRun,
				AutoUpdate = info.AutoUpdate,
				MinerName = info.MinerName,
				State = info.State,
				StateTime = info.StateTime,
				FirstCoin = info.FirstCoin,
				FirstPool = info.FirstPool,
				FirstWallet = info.FirstWallet,
				BackupPool = info.BackupPool,
				SecondCoin = info.SecondCoin,
				SecondPool = info.SecondPool,
				SecondWallet = info.SecondWallet,
				Dcri = info.Dcri,
				MinFan = info.MinFan,
				MaxFan = info.MaxFan,
				AdvParams = info.AdvParams,
				MinTemp = info.MinTemp,
				MaxTemp = info.MaxTemp,
				Eres = info.Eres,
				MinSpeed = info.MinSpeed,
				HighTempStrategy = info.HighTempStrategy,
				HighTempWarn = info.HighTempWarn,
				LowStrengthWarn = info.LowStrengthWarn,
				CoreType = info.CoreType
			};
		}

		return result;
	}

	#endregion
}
